package com.avecsys.quotemanager.mbeans.buyer;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.icefaces.ace.event.SelectEvent;
import org.icefaces.ace.json.JSONException;
import org.icefaces.ace.json.JSONObject;

import com.avecsys.quotemanager.dao.common.EnumActor;
import com.avecsys.quotemanager.exception.BusinessException;
import com.avecsys.quotemanager.mbeans.core.BaseMBean;
import com.avecsys.quotemanager.mbeans.supplier.SupplierUsersListMBean;
import com.avecsys.quotemanager.pojos.BuyerMasterPojo;
import com.avecsys.quotemanager.pojos.SupplierMasterPojo;
import com.avecsys.quotemanager.pojos.SupplierUsersPojo;
import com.avecsys.quotemanager.pojos.UserMasterPojo;
import com.avecsys.quotemanager.service.buyer.BuyerMasterService;
import com.avecsys.quotemanager.utils.MessageBoardUtil;
import com.avecsys.quotemanager.utils.TransactionStatus;

public class BuyerMasterListMBean extends BaseMBean implements Serializable
{

	private final Log log = LogFactory.getLog(BuyerMasterListMBean.class);

	private List<BuyerMasterPojo> buyerMasterPojoList = new ArrayList<BuyerMasterPojo>();

	private BuyerMasterService buyerMasterService;

	/**
	 * Default Constructor
	 */
	public BuyerMasterListMBean()
	{
		super();
	}

	/**
	 * @return the buyerMasterPojoList
	 */
	public List<BuyerMasterPojo> getBuyerMasterPojoList()
	{
		return buyerMasterPojoList;
	}

	/**
	 * @return the buyerMasterService
	 */
	public BuyerMasterService getBuyerMasterService()
	{
		return buyerMasterService;
	}

	/**
	 * @param buyerMasterPojoList the buyerMasterPojoList to set
	 */
	public void setBuyerMasterPojoList(List<BuyerMasterPojo> buyerMasterPojoList)
	{
		this.buyerMasterPojoList = buyerMasterPojoList;
	}

	/**
	 * @param buyerMasterService the supplierMasterService to set
	 */
	public void setBuyerMasterService(BuyerMasterService buyerMasterService)
	{
		this.buyerMasterService = buyerMasterService;
	}

	public void init()
	{
		try
		{
			UserMasterPojo userMasterPojo = getSignedInUser();
			if (userMasterPojo != null)
			{
				for (SupplierUsersPojo supplierUsersPojo : userMasterPojo.getSupplierUsersPojo())
				{
					SupplierMasterPojo supplierMasterPojo = supplierUsersPojo.getSupplierMasterPojo();
					setBuyerMasterPojoList(buyerMasterService.getAllBuyers(supplierMasterPojo));
				}
			}

		}
		catch (BusinessException e)
		{
			log.error(e);
		}
	}

	/**
	 * @param actionEvent
	 */
	public void actionDeleteBuyerMaster(ActionEvent actionEvent)
	{
		String requestJSON = getMessageDialog().getRequestJSON();
		JSONObject requestJSONObject;
		String buyerId = null;
		try
		{
			requestJSONObject = new JSONObject(requestJSON);
			buyerId = (String) requestJSONObject.get("buyerId");

			BuyerMasterPojo buyerMasterPojo = buyerMasterService.getBuyerMasterById(new Long(buyerId));
			if (buyerMasterPojo != null)
			{
				TransactionStatus transactionStatus = buyerMasterService.deleteBuyerMaster(buyerMasterPojo);
				init();
				MessageBoardUtil.publishTransactionStatusMessage(
						transactionStatus,
							EnumActor.BUYER,
							"messageBoardId",
							"",
							new Object[]
							{ buyerMasterPojo.getCompanyMasterPojo().getCompanyName() });
			}
			else
			{

			}
		}
		catch (BusinessException e)
		{
			log.error(e);
		}
		catch (Exception e)
		{
			log.error(e);
		}
		getMessageDialog().hideDialog();
	}

	/**
	 * @param actionEvent
	 */
	public void confirmDeleteAction(ActionEvent actionEvent)
	{
		FacesContext faceContext = FacesContext.getCurrentInstance();
		Map<String, String> paramValues = faceContext.getExternalContext().getRequestParameterMap();
		String buyerId = (String) paramValues.get("buyerId");
		String companyName = (String) paramValues.get("companyName");
		JSONObject requestJSONObject = new JSONObject();
		try
		{
			requestJSONObject.put("buyerId", buyerId);
			requestJSONObject.put("companyName", companyName);
			getMessageDialog().setRequestJSON(requestJSONObject.toString());
			getMessageDialog().setMessage("Are you sure to delete '" + companyName + "' ?");
		}
		catch (JSONException e)
		{
			log.error(e);
		}
		getMessageDialog().showDialog();
	}

	/**
	 * Method to edit the buyer master information
	 * 
	 * @return
	 */
	public String editBuyerMaster()
	{
		// Call Common routine
		executePreRoutines();

		FacesContext faceContext = FacesContext.getCurrentInstance();
		Map<String, String> paramValues = faceContext.getExternalContext().getRequestParameterMap();
		String buyerId = (String) paramValues.get("buyerId");
		try
		{
			BuyerMasterPojo buyerMasterPojo = buyerMasterService.getBuyerMasterById(new Long(buyerId));
			BuyerMasterMBean buyerMasterMBean = (BuyerMasterMBean) faceContext.getApplication()
					.evaluateExpressionGet(faceContext, "#{buyerMasterMBean}", BuyerMasterMBean.class);
			buyerMasterMBean.setBuyerMasterPojo(buyerMasterPojo);
			buyerMasterMBean.setScreenModeEdit(true);
		}
		catch (BusinessException e)
		{
			log.error(e);
		}
		catch (Exception e)
		{
			log.error(e);
		}
		return "success";
	}

	/**
	 * @param selectEvent
	 */
	public String showSelectedBuyerUsers(SelectEvent selectEvent)
	{
		MessageBoardUtil.resetMessageBoard();
		FacesContext faceContext = FacesContext.getCurrentInstance();
		BuyerMasterPojo buyerMasterPojo = (BuyerMasterPojo) selectEvent.getObject();
		BuyerUsersListMBean buyerUsersListMBean = (BuyerUsersListMBean) faceContext.getApplication()
				.evaluateExpressionGet(
						faceContext,
							"#{buyerUsersListMBean}",
							BuyerUsersListMBean.class);
		try
		{
			buyerUsersListMBean.loadAllUsersByBuyer(buyerMasterPojo);
		}
		catch (BusinessException e)
		{
			log.error(e);
		}
		return "showBuyerUsers";
	}
}
